package com.xuehuitao.imagetoexcel.fileController;

import com.xuehuitao.imagetoexcel.dto.ConvertResult;
import com.xuehuitao.imagetoexcel.fileService.ImageToExcelConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/api/image-to-excel")
public class ImageToExcelController {
    private static final Logger logger = LoggerFactory.getLogger(ImageToExcelConverter.class);

@Autowired
private ImageToExcelConverter imageToExcelConverter;



    // 可选：提供一个上传图片并处理的方法
    @PostMapping("/upload")
    public ConvertResult uploadImage(@RequestParam("file") MultipartFile file,
                              @RequestParam int cellSize) throws IOException {
        if (file.isEmpty()) {
            throw new RuntimeException("上传的图片文件为空");
        }

        // 创建临时文件
        File tempFile = File.createTempFile("upload-", ".tmp");
        file.transferTo(tempFile); // 将上传文件转存到临时文件
        logger.info("上传的文件单元格尺寸为:"+cellSize);
        try {
            ConvertResult convertResult = imageToExcelConverter.convertImageToExcelNew(tempFile.getAbsolutePath(), cellSize);
            return convertResult;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("转换失败：" + e.getMessage());
        }
    }

}
